using System;
using Glue;
using Glue.Converters;
using Glue.Internals.Verification;
using Xunit;

namespace GlueTests.MapperVerificationTests
{
    public class When_converter_dont_produce_different_values
    {
        public class DomainPerson { public int Id { get; set; } }
        public class GuiPerson { public String Id { get; set; } }

        private readonly Mapping<DomainPerson, GuiPerson> mapping;
        private readonly MapperVerification<DomainPerson, GuiPerson> verification;

        public When_converter_dont_produce_different_values()
        {
            mapping = new Mapping<DomainPerson, GuiPerson>();
            verification = new MapperVerification<DomainPerson, GuiPerson>(mapping);
        }

        [Fact]
        public void If_same_value_as_reciever_Should_throw_meaningful_exception_because_we_cant_validate_mapping()
        {
            mapping.AddConverter(new ReturnSameConverter("0",0));
            mapping.AutoRelateEqualNames();
            Assert.Throws(typeof(GlueException), 
                          () => verification.AssertMapsCorrectlyTowards(new GuiPerson { Id = "0" }));

        }

        [Fact]
        public void If_different_value_than_reciever_Should_map_anyway()
        {
            mapping.AddConverter(new ReturnSameConverter("1", 1));
            mapping.AutoRelateEqualNames();
            verification.AssertMapsCorrectlyTowards(new GuiPerson { Id = "0" });
        }
    }

    public class ReturnSameConverter : BaseConverter<int, string>
    {
        private readonly string stringToReturn;
        private readonly int intToReturn;

        public ReturnSameConverter(String stringToReturn, int intToReturn)
        {
            this.stringToReturn = stringToReturn;
            this.intToReturn = intToReturn;
        }

        public override string MapTowardsRight(int from, string to)
        {
            return stringToReturn;
        }

        public override int MapTowardsLeft(string from, int to)
        {
            return intToReturn;
        }
    }
}